%% test script
%
%%
clear all
clc
close all
%%
img=imread('lena.jpg');
imgsz=size(img)

figure(1);
imshow(img);

row = imgsz(1);
col = imgsz(2);

Meanimg=[repmat(mean(img(:,:,1),2),1,col);repmat(mean(img(:,:,2),2),1,col);repmat(mean(img(:,:,3),2),1,col)];
Mn=[img(:,:,1);img(:,:,2);img(:,:,3)]-Meanimg;
%Mn=[img(:,:,1)-repmat(mean(img(:,:,1),2),1,col);img(:,:,2)-repmat(mean(img(:,:,2),2),1,col);img(:,:,3)-repmat(mean(img(:,:,3),2),1,col)];

[U,S,V]=svd(Mn);

B=zeros(row*3,col);

sumSi=0;
SumS=sum(sum(S));
for i=1:size(S,1)

	sumSi=S(i,i)+sumSi;	
	if sumSi >= SumS
                break;
        end
end
diagnum=i
B=U(:,1:i)*S(1:i,1:i)*V(:,1:i)';
%for i=1:diagnum

%	B=S(i,i)*U(:,i)*V(:,i)'+B;

%end

imgnew(:,:,1)=B(1:row,:)+Meanimg(1:row,:);
imgnew(:,:,2)=B(row+1:2*row,:)+Meanimg(row+1:2*row,:);
imgnew(:,:,3)=B(2*row+1:3*row,:)+Meanimg(2*row+1:3*row,:);

imgnew=uint8(imgnew);

figure(2)
imshow(imgnew)
